/**
 * Created by wangle on 2018/4/17.
 */
qui(function (require, exports, module) {
    RecordCol.Util = $.extend(true, {}, EpgWeb.Util, {
        formatter: {
            operator_plan_list: function (v, o, r) {
                var htmlArr = [];
                htmlArr.push("<a href='javascript:;' onclick='RecordCol.Util.openColModifyDlg(" + r.id + ")'>修改</a>&nbsp;&nbsp;&nbsp;");
                htmlArr.push("<a href='javascript:;' onclick='RecordCol.Util.delCol(" + r.id + ")'>删除</a>");
                return htmlArr.join("");
            },
            hasAuth: function (v, o, r) {
                return v == 1 ? "有" : "无";
            },
            hasPublic: function (v, o, r) {
                return v == 1 ? "是" : "否";
            },
            category: function (v, o, r) {
                var resultData = RecordCol.Dao.getDataByName("lego_category");
                var ret = "";
                $.each(resultData.data, function (i, element) {
                    if (element.id == v) {
                        ret = element.name;
                        return false;
                    }
                })
                return ret;
            },
            auth: function (v, o, r) {
                return v == 0 ? "黑名单":"白名单";
            }
        },
        reloadHandler: function (data) {
            if (data != null) {
                EpgWeb.Plugin.showTips({content: data.msg, type: data.code == "A00000" ? "success" : "error"});
                if (data.code == "A00000") {
                    $("#list").trigger("reloadGrid")
                }
            } else {
                console.log("callbackHandler no return data");
            }
        },
        checkValidColMode: function (entity) {
            console.log(entity)
            if (isNaN(entity.channelId) || entity.channelName=='') {
                RecordCol.Util.logTips({ theme: "error", content: "请填写有效频道"});
                return false;
            }
            if (entity.colName =='') {
                RecordCol.Util.logTips({ theme: "error", content: "请填写有效栏目"});
                return false;
            }
            if (entity.categoryId =='') {
                RecordCol.Util.logTips({ theme: "error", content: "请填写有效分类"});
                return false;
            }
            return true;
        },
        prepareColMode: function () {
            var sendData = {};
            var parentDom = $("form[role=addForm]");
            sendData.channelId = parseInt(parentDom.find("input[name=channel_name]").suggest("getValue"));
            sendData.channelName = parentDom.find("input[name=channel_name]").val();

            sendData.colName = parentDom.find("input[name=col_name]").val().trim();
            sendData.status = 1;
            var selectArr = parentDom.find("select[role=query-select]")
            // $.each(selectArr, function (i, element) {
            //     if (element.name != "query-repeat-code") {
            //         if (element.value.trim().length > 0) {
            //             // params [$(element).attr("name")] = element.value.trim()
            //             sendData.categoryId = element.value.trim();
            //         }
            //     }
            // });
            sendData.categoryId = parentDom.find("select[name=category_id]").val();
            sendData.hasAuth = parseInt(parentDom.find("select[name=hasAuth]").val());
            sendData.hasPublic = parseInt(parentDom.find("select[name=enable]").val());
            return sendData;
        },
        sendAddColPlan: function () {
            var sendData = RecordCol.Util.prepareColMode();
            var flag = RecordCol.Util.checkValidColMode(sendData);
            if (flag) {
                $("form[role=addForm]").dialog("close")
                RecordCol.Dao.addRecordCol({data: JSON.stringify(sendData)}, function (data) {
                    if (data != null && data.code == "A00000") {
                        RecordCol.Util.reloadHandler(data)
                    } else {
                        EpgWeb.Plugin.showTips({type: "error", content: "创建失败"});
                    }
                })
            }
        },
        sendEditColPlan: function () {
            var sendData = RecordCol.Util.prepareColMode();
            var tempId = parseInt($("form[role=addForm]").find("input[name=edit_col_id]").val());
            if (tempId > 0) {
                sendData.id = tempId;
            }
            var flag = RecordCol.Util.checkValidColMode(sendData);
            if (flag) {
                $("form[role=addForm]").dialog("close");
                RecordCol.Dao.editRecordCol({data: JSON.stringify(sendData)}, function (data) {
                    if (data != null && data.code == "A00000") {
                        RecordCol.Util.reloadHandler(data)
                    } else {
                        EpgWeb.Plugin.showTips({type: "error", content: "创建失败"});
                    }
                })
            }
        },
        delCol: function (entityId) {
            console.log("wait to do");
            SystemDialog.queryConfirm("确认删除？", function (flag) {
                if (flag === true) {
                    RecordCol.Dao.deleteRecordCol({id: entityId}, function (data) {
                        RecordCol.Util.reloadHandler(data);
                    })
                }
            })
        },
        doAddFormReset: function () {
            var parentDom = $("form[role=addForm]");
            parentDom[0].reset();
            parentDom.find("input[name=col_name]").val(null);
            parentDom.find("input[name=channel_name]").val(null);
            parentDom.find("select[name=hasAuth]").val(null);
            parentDom.find("select[name=enable]").val(null);
            parentDom.find("select[name=category_id]").val(null);
        },
        logTips: function (options) {
            EpgWeb.Plugin.showTips(options);
        },
        doAddFormFillData: function (element) {
            var parentDom = $("form[role=addForm]");
            parentDom.find("input[name=edit_col_id]").val(element.id);
            parentDom.find("input[name=col_name]").val(element.colName);
            parentDom.find("input[name=channel_name]").suggest("setValue", {id: element.channelId, label: element.channelName}).attr('disabled','disabled');
            parentDom.find("select[name=hasAuth]").val(element.hasAuth);
            parentDom.find("select[name=enable]").val(element.hasPublic);
            parentDom.find("select[name=category_id]").val(element.categoryId);
        },
        openColModifyDlg: function (entityId) {
            RecordCol.Util.doAddFormReset();
            var parentDom = $("form[role=addForm]");
            if (entityId > 0) {
                RecordCol.Dao.getRecordColInfo({id: entityId}, function (data) {
                    if (data != null && data.code == "A00000") {
                        // RecordCol.Dao.getRecordChannelList({paramsObj: JSON.stringify({EQL_id: data.data.channelId}), dataMode: "simple"}, function (gdata) {
                        //     if (gdata != null && gdata.code == "A00000") {
                        //         parentDom.dialog("open");
                        //         parentDom.find("button[name=add_plan_btn]").hide();
                        //         data.data.channelName = gdata.data.result[0].channelName;
                        //         RecordCol.Util.doAddFormFillData(data.data);
                        //     }
                        // })
                        RecordCol.Util.doAddFormFillData(data.data);
                        parentDom.dialog("open");
                        parentDom.find("button[name=add_col_btn]").hide();
                        parentDom.find("button[name=edit_col_btn]").show();
                    } else {
                        RecordCol.Util.logTips({theme: "error", content: "访问数据失败"});
                    }
                });
            } else {
                parentDom.dialog("open");
                parentDom.find("button[name=edit_col_btn]").hide();
                parentDom.find("button[name=add_col_btn]").hide();
            }
            $(".ui-dialog-title").html("修改栏目");
        }
    });
});